package cn.edu.scau.dao;

import cn.edu.scau.entity.Dish;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;


@Mapper
@CacheNamespace
public interface DishMapper {
	/**
	 * 根据膳食编号删除膳食信息
	 * 
	 * @param dishid
	 * @return
	 */
	@Delete({ "delete from dish", "where dishID = #{dishid,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer dishid);

	/**
	 * 全参新增膳食信息：不考虑为null情况
	 * 
	 * @param record
	 * @return
	 */
	@Insert({ "insert into dish (dishID, dishName, ", "dishPrice, dishMaterial, ", "dishType, dishFeatures)",
			"values (#{dishid,jdbcType=INTEGER}, #{dishname,jdbcType=VARCHAR}, ",
			"#{dishprice,jdbcType=DECIMAL}, #{dishmaterial,jdbcType=VARCHAR}, ",
			"#{dishtype,jdbcType=VARCHAR}, #{dishfeatures,jdbcType=VARCHAR})" })
	int insert(Dish record);

	/**
	  * 全参新增膳食信息：考虑为null情况，如果为空，不向数据库插入数据
	 * 
	 * @param record
	 * @return
	 */
	@InsertProvider(type = DishSqlProvider.class, method = "insertSelective")
	int insertSelective(Dish record);

	/**
	 * 根据膳食编号查询膳食信息
	 * 
	 * @param dishid
	 * @return
	 */
	@Select({ "select", "dishID, dishName, dishPrice, dishMaterial, dishType, dishFeatures", "from dish",
			"where dishID = #{dishid,jdbcType=INTEGER}" })
	@Results({ @Result(column = "dishID", property = "dishid", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "dishName", property = "dishname", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dishPrice", property = "dishprice", jdbcType = JdbcType.DECIMAL),
			@Result(column = "dishMaterial", property = "dishmaterial", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dishType", property = "dishtype", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dishFeatures", property = "dishfeatures", jdbcType = JdbcType.VARCHAR) })
	Dish selectByPrimaryKey(Integer dishid);

	/**
	  * 查询所有膳食信息 
	 * @return
	 */
	@Select({ "select", "dishID, dishName, dishPrice, dishMaterial, dishType, dishFeatures", "from dish" })
	List<Dish> selectAll();

	/**
	  * 根据膳食名称、类别、特点组合模糊查询
	 * @param dishname
	 * @param dishtype
	 * @param dishfeatures
	 * @return
	 */
    @SelectProvider(type = DishSqlProvider.class,method = "findByNameTypeFeatures")
    List<Dish> findByNameTypeFeatures(@Param("dishname")String dishname,@Param("dishtype")String dishtype,@Param("dishfeatures")String dishfeatures);
    
	/**
	 * 全参修改膳食信息：考虑为null情况，如果为空，不会修改数据库中内容
	 * 
	 * @param record
	 * @return
	 */
	@UpdateProvider(type = DishSqlProvider.class, method = "updateByPrimaryKeySelective")
	int updateByPrimaryKeySelective(Dish record);

	/**
	  * 全参修改膳食信息：不考虑为null情况，如果为空，数据库中对应内容将重置为null
	 * 
	 * @param record
	 * @return
	 */
	@Update({ "update dish", "set dishName = #{dishname,jdbcType=VARCHAR},",
			"dishPrice = #{dishprice,jdbcType=DECIMAL},", "dishMaterial = #{dishmaterial,jdbcType=VARCHAR},",
			"dishType = #{dishtype,jdbcType=VARCHAR},", "dishFeatures = #{dishfeatures,jdbcType=VARCHAR}",
			"where dishID = #{dishid,jdbcType=INTEGER}" })
	int updateByPrimaryKey(Dish record);
}